package org.chen.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.chen.model.CommentBatch;
import org.chen.enu.TaskStatus;
import org.chen.model.CommentTask;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface CommentTaskMapper extends BaseMapper<CommentTask> {

    /**
     * 批量插入任务
     */
    @Insert({"<script>",
            "INSERT INTO comment_task (" +
                    "id, batch_no, target_url, comment_content, nickname, email, " +
                    "website_url, is_executed, status, retry_count, create_time, update_time" +
                    ") VALUES " +
                    "<foreach collection='list' item='item' separator=','>" +
                    "(" +
                    "#{item.id}, #{item.batchNo}, #{item.targetUrl}, " +
                    "#{item.commentContent}, #{item.nickname}, #{item.email}, " +
                    "#{item.websiteUrl}, #{item.isExecuted}, #{item.status}, " +
                    "#{item.retryCount}, #{item.createTime}, #{item.updateTime}" +
                    ")" +
                    "</foreach>",
            "</script>"})
    void batchInsert(@Param("list") List<CommentTask> tasks);

    /**
     * 更新任务状态
     */
    @Update("UPDATE comment_task SET new_comment_url = #{newUrl}, update_time = #{updateTime} " +
            "WHERE id = #{taskId}")
    void updateNewUrl(@Param("taskId") Long taskId,
                      @Param("newUrl") String newUrl,
                      @Param("updateTime") LocalDateTime updateTime);

    @Update("""
        UPDATE comment_task 
        SET status = #{batch.status},
            retry_count = 0,
            error_message = null,
            new_comment_url = null,
            comment_content = #{batch.commentContent},
            nickname = #{batch.nickname},
            email = #{batch.email},
            website_url = #{batch.websiteUrl},
            update_time = NOW()
        WHERE batch_no = #{batchNo}
    """)
    void resetTasksForBatch(@Param("batchNo") String batchNo,
                            @Param("batch") CommentBatch batch);

    /**
     * 基于时间查询待处理任务
     */
    @Select({
            "<script>",
            "SELECT * FROM comment_task ",
            "WHERE batch_no = #{batchNo} ",
            "AND status = #{status} ",
            "<if test='lastTime != null'>",
            "AND create_time > #{lastTime} ",
            "</if>",
            "ORDER BY create_time ASC ",
            "LIMIT #{limit}",
            "</script>"
    })
    List<CommentTask> selectPendingTasksByTime(
            @Param("batchNo") String batchNo,
            @Param("status") String status,
            @Param("lastTime") LocalDateTime lastTime,
            @Param("limit") Integer limit);

    /**
     * 统计待处理任务数
     */
    @Select("SELECT COUNT(*) FROM comment_task " +
            "WHERE batch_no = #{batchNo} " +
            "AND status = 'PENDING'")
    long countPendingTasks(@Param("batchNo") String batchNo);
}
